package com.augurit.common.system.web.org;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.augurit.common.base.action.ExtCrudActionSupport;
import com.augurit.common.base.extmodel.inner.GroupTreeNode;
import com.augurit.common.constant.OrgConstant;
import com.augurit.common.system.convert.OmGroupConverter;
import com.augurit.common.system.service.org.IAcRoleService;
import com.augurit.common.system.service.org.IOmGroupService;
import com.augurit.common.system.service.org.IOmOrgService;
import com.augurit.common.system.service.org.IOmPositionService;
import com.augurit.common.system.service.org.IOmUserService;
import com.augurit.common.system.util.WebContextUtils;
import com.augurit.common.system.web.org.form.OmGroupForm;
import com.augurit.common.system.web.org.form.OmOrgForm;
import com.augurit.common.system.web.org.form.OmPositionForm;
import com.augurit.common.system.web.org.form.OmUserForm;
import com.augurit.common.util.ExtUtils;

/**
 * 工作组管理Action类
 */
@SuppressWarnings("serial")
@Results({
	@Result(name = ExtCrudActionSupport.RELOAD, location = "om-group.action", type = "redirect"),
	@Result(name = ExtCrudActionSupport.SUCCESS, location = "/common/system/org/om-group.jsp"),
})
public class OmGroupAction extends ExtCrudActionSupport<OmGroupForm> {

	// Service 引用
	@Autowired
	private IOmOrgService omOrgService;
	@Autowired
	private IOmGroupService omGroupService;
	@Autowired
	private IOmPositionService omPositionService;
	@Autowired
	private IOmUserService omUserService;
	@Autowired
	private IAcRoleService acRoleService;
	
	
	
	//属性
	private Page<OmGroupForm> page = new Page<OmGroupForm>();
	private OmGroupForm omGroupForm;
	
	private Long groupId;		//工作组id
	private Long[] groupIds;	//工作组id数组
	private Long orgId;			//机构id
	private Long posId;			//岗位id
	private String posIds;		//岗位id数组
	private Long roleId;		//角色id
	private Long[] userIds;		//用户id
	
	/**
	 * 跳转到JSP页面
	 */
	public String execute() throws Exception{
		return SUCCESS;
	}
	
	/**
	 * 分页显示、查询指定机构下工作组
	 * orgId: 机构id,当机构id=-1L是表示查询系统中所有的工作组
	 */
	public String list() throws Exception {
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		Page<OmGroupForm> pg = omOrgService.searchGroupsInOrgByPriv(this.getLoginUserId(), orgId, omGroupForm, page, WebContextUtils.getSysCodeBuffer(this.getApplication()));
		this.extRenderGridJson(pg);
		return null;
	}
	
	/**
	 * 添加、修改工作组
	 */
	@Override
	public String save() throws Exception {
		this.omGroupService.addGroupCascade(omGroupForm);
		this.extRenderSuccess();
		return null;
	}
	
	/**
	 * 编辑已有工作组
	 */
	@Override
	public String input() throws Exception {
		OmGroupForm form = omGroupService.getForm(groupId);
		this.extRenderFormJson(form);
		return null;
	}

	/**
	 * 删除一个或多个工作组
	 */
	@Override
	public String delete() throws Exception {
		omGroupService.deleteGroupCascade(groupIds);
		this.extRenderSuccess();
		return null;
	}
	
	/**
	 * 给工作组添加用户
	 * @return
	 * @throws Exception
	 */
	public String addUsersToGroup() throws Exception {
		omGroupService.addUsersToGroup(groupId, userIds);
		this.extRenderSuccess();
		return null;
	}
	
	/**
	 * 向工作组岗位中添加用户
	 * @return
	 * @throws Exception
	 */
	public String addUsersToGroupPos() throws Exception{
		omGroupService.addUsersToGroupPos(posId, groupId, userIds);
		this.extRenderSuccess();
		return null;
	}
	
	/**
	 * 获取工作组的直属工作组
	 * @return
	 * @throws Exception
	 */
	public String getGroupsByParentGroupId() throws Exception{
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		Page<OmGroupForm> pg = omGroupService.getDirectGroupsByGroup(groupId, page);
		this.extRenderGridJson(pg);
		return null;
	}
	
	//获取机构结点的孩子结点
	private List<GroupTreeNode> getChildNodesOfOrg(GroupTreeNode node){
		List<GroupTreeNode> result = new ArrayList();
		//获取机构下的直属工作组
		if(omUserService.isSystemAdmin(this.getLoginUserId()) || omOrgService.isOrgAdmin(this.getLoginUserId(), node.getOrgId())){
			List<OmGroupForm> groupList  = omOrgService.getDirectGroups(node.getOrgId());
			result.addAll(OmGroupConverter.convertGroupToTreeNode(groupList, node));
		}
		
		//获取机构下拥有工作组的子机构
		List<OmOrgForm>  orgList = omOrgService.getNextLevelOrgOwendGroup(this.getLoginUserId(),node.getOrgId());
		
		List<GroupTreeNode> list = OmGroupConverter.convertOrgToTreeNode(orgList);
		result.addAll(list);
		return result;
	}
	
	//获取工作组结点的孩子结点
	private  List<GroupTreeNode> getChildNodesOfGroup(GroupTreeNode node){
		List<GroupTreeNode> result = null;
		List<OmGroupForm> groupList = omGroupService.getDirectGroupsByGroup(node.getGroupId());
		result = OmGroupConverter.convertGroupToTreeNode(groupList, node);
		
		List<OmPositionForm> posList = omGroupService.getPositions(node.getGroupId());
		result.addAll(OmGroupConverter.convertPosToTreeNode(posList, node));
		
		List<OmUserForm> userList = omGroupService.getUsersNotHavePos(node.getGroupId());
		result.addAll(OmGroupConverter.convertUserToTreeNode(userList));
		return result;
	}
	
	//获取岗位的孩子结点
	private List<GroupTreeNode> getChildNodesOfPos(GroupTreeNode node){
		List<GroupTreeNode> result = null;
		List<OmPositionForm> posList = omPositionService.getChildPositions(node.getPosId());
		result = OmGroupConverter.convertPosToTreeNode(posList, node);
		List<OmUserForm> userList = omPositionService.getUsersByPosId(node.getPosId());
		result.addAll(OmGroupConverter.convertUserToTreeNode(userList));
		return result;
	}
	
	/**
	 * 根据当前用户权限获取工作组树节点的子节点（机构、工作组、岗位、用户）列表
	 */
	public String getChildrenByPriv(){
		List<GroupTreeNode> result = null;
		
		// 根据Request参数构造GroupTreeNode对象
		GroupTreeNode groupTreeNode = GroupTreeNode.build(super.getRequest());
		
		//是根结点
		if(groupTreeNode.getPartyType().equals(OrgConstant.PARTY_TYPE_ROOT)){
			//获取用户具有管理权限的工作组的最顶级机构
			List<OmOrgForm> list = omOrgService.getTopOrgsOwnedGroup(this.getLoginUserId());
			result = OmGroupConverter.convertOrgToTreeNode(list);
		}
		//机构节点
		else if(groupTreeNode.getPartyType().equals(OrgConstant.PARTY_TYPE_ORG)){
			result = this.getChildNodesOfOrg(groupTreeNode);
		}
		//工作组结点
		else if(groupTreeNode.getPartyType().equals(OrgConstant.PARTY_TYPE_GROUP)){
			result = this.getChildNodesOfGroup(groupTreeNode);
		}
		//岗位结点
		else{
			result = this.getChildNodesOfPos(groupTreeNode);
		}
		this.extRenderListJson(result, false);
		return null;
	}
	
	/**
	 * 获取与指定角色关联的工作组
	 * @return
	 * @throws Exception
	 */
	public  String getGroupsByRole() throws Exception{ 
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		Page<OmGroupForm> pg = acRoleService.getGroupsByRole(page, roleId, WebContextUtils.getSysCodeBuffer(this.getApplication()));
		this.extRenderGridJson(pg);
		return null;
	}
	
	/**
	 * 获取与指定角色不关联的工作组
	 * @return
	 */
	public String getGroupsNotAssociateRole() throws Exception{
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		Page<OmGroupForm> pg = acRoleService.getGroupsNotAssociateRole(page, roleId, omGroupForm, WebContextUtils.getSysCodeBuffer(this.getApplication()));
		this.extRenderGridJson(pg);
		return null;
	}
	
	public OmGroupForm getModel(){
		return omGroupForm;
	}
	
	@Override
	protected void prepareModel() throws Exception {
		if(groupId != null)
			omGroupForm = omGroupService.get(groupId);
		else
			omGroupForm = new OmGroupForm();
	}
	
	public OmGroupForm getOmGroupForm() {
		return omGroupForm;
	}

	public void setOmGroupForm(OmGroupForm omGroupForm) {
		this.omGroupForm = omGroupForm;
	}

	public Long getPosId() {
		return posId;
	}

	public void setPosId(Long posId) {
		this.posId = posId;
	}

	public Long getOrgId() {
		return orgId;
	}

	public void setOrgId(Long orgId) {
		this.orgId = orgId;
	}

	public String getPosIds() {
		return posIds;
	}

	public void setPosIds(String posIds) {
		this.posIds = posIds;
	}

	public Long getGroupId() {
		return groupId;
	}

	public void setGroupId(Long groupId) {
		this.groupId = groupId;
	}

	public Long[] getGroupIds() {
		return groupIds;
	}

	public void setGroupIds(Long[] groupIds) {
		this.groupIds = groupIds;
	}

	public Long[] getUserIds() {
		return userIds;
	}

	public void setUserIds(Long[] userIds) {
		this.userIds = userIds;
	}

	public Long getRoleId() {
		return roleId;
	}

	public void setRoleId(Long roleId) {
		this.roleId = roleId;
	}
}
